package com.moshme.project.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.moshme.common.utils.StringUtils;
import com.moshme.project.domain.ProjectInfoUser;
import com.moshme.project.mapper.ProjectInfoUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.moshme.project.mapper.ProjectInfoMapper;
import com.moshme.project.domain.ProjectInfo;
import com.moshme.project.service.ProjectInfoService;
import org.springframework.transaction.annotation.Transactional;


/**
 * 项目信息Service业务层处理
 *
 * @author moshme
 * @date 2023-08-11
 */
@Service
public class ProjectInfoServiceImpl extends ServiceImpl<ProjectInfoMapper, ProjectInfo> implements ProjectInfoService {
    @Autowired
    private ProjectInfoMapper projectInfoMapper;
    @Autowired(required = false)
    private ProjectInfoUserMapper infoUserMapper;

    /**
     * 查询项目信息
     *
     * @param projectId 项目信息主键
     * @return 项目信息
     */
    @Override
    public ProjectInfo selectProjectInfoByProjectId(Long projectId) {
        return projectInfoMapper.selectProjectInfoByProjectId(projectId);
    }

    /**
     * 查询项目信息列表
     *
     * @param projectInfo 项目信息
     * @return 项目信息
     */
    @Override
    public List<ProjectInfo> selectProjectInfoList(ProjectInfo projectInfo) {
        return projectInfoMapper.selectProjectInfoList(projectInfo);
    }

    /**
     * 新增项目信息
     *
     * @param projectInfo 项目信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertProjectInfo(ProjectInfo projectInfo) {

        int rows = projectInfoMapper.insert(projectInfo);

        Long[] userIds = projectInfo.getUserIds();
        Long geo = projectInfo.getGeo();
        boolean flag = false;
        for (Long userId : userIds) {
            if(userId.equals(geo)){
                flag = true;
                break;
            }
        }
        if(!flag){
            Long[] newIds = Arrays.copyOf(userIds, userIds.length + 1);
            newIds[userIds.length] = geo;
            userIds = newIds;
            projectInfo.setUserIds(userIds);
        }

        //新增项目员工关联
        insertProjectUser(projectInfo);
        return rows;
    }

    /**
     * 新增项目员工关联
     *
     * @param projectInfo
     */
    private void insertProjectUser(ProjectInfo projectInfo) {
        Long[] userIds = projectInfo.getUserIds();
        if (StringUtils.isNotEmpty(userIds) && userIds.length > 0) {
            ArrayList<ProjectInfoUser> list = new ArrayList<>();
            for (Long userId : userIds) {
                ProjectInfoUser projectInfoUser = new ProjectInfoUser();
                projectInfoUser.setUserId(userId);
                projectInfoUser.setProjectId(projectInfo.getProjectId());
                list.add(projectInfoUser);
            }

            infoUserMapper.batchProjectUser(list);
        }
    }

    /**
     * 修改项目信息
     *
     * @param projectInfo 项目信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateProjectInfo(ProjectInfo projectInfo) {
        Long projectId = projectInfo.getProjectId();
        //删除项目人员关联
        infoUserMapper.deleteById(projectId);

        insertProjectUser(projectInfo);
        return projectInfoMapper.updateById(projectInfo);
    }

    /**
     * 批量删除项目信息
     *
     * @param projectIds 需要删除的项目信息主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteProjectInfoByProjectIds(Long[] projectIds) {
        //删除要删除项目的所有关联
        for (Long projectId : projectIds) {
            infoUserMapper.deleteById(projectId);
        }
        return projectInfoMapper.deleteProjectInfoByProjectIds(projectIds);
    }

    /**
     * 删除项目信息信息
     *
     * @param projectId 项目信息主键
     * @return 结果
     */
    @Override
    public int deleteProjectInfoByProjectId(Long projectId) {
        return projectInfoMapper.deleteProjectInfoByProjectId(projectId);
    }
}

